using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Meta
{
	/// <summary>
	/// Summary description for NumberPicker.
	/// </summary>
	public class NumberPicker : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.TrackBar trackBar;
		private System.Windows.Forms.Label title;
		private System.Windows.Forms.Label minLabel;
		private System.Windows.Forms.Label maxLabel;
		private System.Windows.Forms.TextBox valueBox;

		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public NumberPicker(string title, int min, int max)
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			this.Name = title;
			this.title.Text = title;
			trackBar.Minimum = min;
			minLabel.Text = min.ToString();
			trackBar.Maximum = max;
			maxLabel.Text = max.ToString();
			trackBar.TickFrequency = (max-min)/4;

			Value = (min+max/2);
		}

		public int Value {
			get {
				return trackBar.Value;
			}
			set {
				trackBar.Value = value;
				valueBox.Text = value.ToString();
			}
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.trackBar = new System.Windows.Forms.TrackBar();
			this.title = new System.Windows.Forms.Label();
			this.valueBox = new System.Windows.Forms.TextBox();
			this.minLabel = new System.Windows.Forms.Label();
			this.maxLabel = new System.Windows.Forms.Label();
			((System.ComponentModel.ISupportInitialize)(this.trackBar)).BeginInit();
			this.SuspendLayout();
			// 
			// trackBar
			// 
			this.trackBar.Location = new System.Drawing.Point(128, 24);
			this.trackBar.Name = "trackBar";
			this.trackBar.Size = new System.Drawing.Size(224, 45);
			this.trackBar.TabIndex = 0;
			this.trackBar.ValueChanged += new System.EventHandler(this.trackBar_ValueChanged);
			// 
			// title
			// 
			this.title.Location = new System.Drawing.Point(8, 8);
			this.title.Name = "title";
			this.title.Size = new System.Drawing.Size(400, 24);
			this.title.TabIndex = 1;
			this.title.Text = "title";
			// 
			// valueBox
			// 
			this.valueBox.Location = new System.Drawing.Point(8, 24);
			this.valueBox.Name = "valueBox";
			this.valueBox.Size = new System.Drawing.Size(64, 20);
			this.valueBox.TabIndex = 2;
			this.valueBox.Text = "textBox1";
			this.valueBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.valueBox_KeyUp);
			// 
			// minLabel
			// 
			this.minLabel.Location = new System.Drawing.Point(88, 24);
			this.minLabel.Name = "minLabel";
			this.minLabel.Size = new System.Drawing.Size(48, 24);
			this.minLabel.TabIndex = 3;
			this.minLabel.Text = "min";
			this.minLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// maxLabel
			// 
			this.maxLabel.Location = new System.Drawing.Point(344, 24);
			this.maxLabel.Name = "maxLabel";
			this.maxLabel.Size = new System.Drawing.Size(48, 24);
			this.maxLabel.TabIndex = 4;
			this.maxLabel.Text = "max";
			this.maxLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// NumberPicker
			// 
			this.Controls.Add(this.maxLabel);
			this.Controls.Add(this.minLabel);
			this.Controls.Add(this.valueBox);
			this.Controls.Add(this.title);
			this.Controls.Add(this.trackBar);
			this.Name = "NumberPicker";
			this.Size = new System.Drawing.Size(400, 56);
			((System.ComponentModel.ISupportInitialize)(this.trackBar)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void trackBar_ValueChanged(object sender, System.EventArgs e) {
			string text = trackBar.Value.ToString();
			if (text != valueBox.Text)
				valueBox.Text = text;
		}

		private void valueBox_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) {
			if (e.KeyCode==Keys.Enter) {
				try {
					int newValue = Int32.Parse(valueBox.Text);
					if (newValue != trackBar.Value) {
						if (newValue<trackBar.Minimum)
							newValue = trackBar.Minimum;
						else if (newValue>trackBar.Maximum)
							newValue = trackBar.Maximum;
						trackBar.Value = newValue;
						valueBox.Text = trackBar.Value.ToString();
						e.Handled = true;
					}
				} catch {
					valueBox.Text = trackBar.Value.ToString();
				}
			}		
		}
	}

	public class NumberPickerThunk : CallInterfaceProcedure {
		NumberPicker numberPicker;

		public NumberPickerThunk(NumberPicker numberPicker) {
			this.numberPicker = numberPicker;
		}

		public override object Call() {
			return numberPicker.Value;
		}
	}
}
